/*
 * DA2.c
 *
 * Created: 2/28/2014 2:00:19 PM
 *  Author: mummm2
 */ 


#include <avr/io.h>
#include <avr/interrupt.h>
void Delay(void);

int main(void)
{
	DDRB = 0xff;		//set all PortB as output
	DDRC = 0xff;		//set all PortC as output
	DDRD = 0x00;		//set all PortD as input
	EIMSK = 0x01;		//set as interrupt catch
	EICRA = 0x02;		//set to grab at falling edge
	sei();
	
	while (TCNT0 <256) {
		Delay();				//delay for .25sec
		TCNT0++;				//increment count
		PORTB=TCNT0;			//export the count to PortB
		if((TCNT0 % 10) == 0) {	//if 10th time
			PORTC ^= (1<<0);	//toggle PC0
			PORTC ^= (1<<2);
		}
		Delay();				//delay for .25sec
	}
}

void Delay(void) {
	OCR1A = 0x7a12;		//(.25secs)(8x10^6MHz)/64 = 31250 = 0x7a12
	TCCR1A = 0;			//CTC mode, WGM=0100
	TCCR1B = 0b00001011;//CS=011, prescaler of 64
	while((TIFR1 & 0x02) == 0) {
		//wait
	}
	TIFR1 = 0x02;		//reset the counter
}

ISR(INT0_vect) {
	TCNT2++;	
	if(TCNT2==0x02) {
		PORTC ^= (1<<1);	//toggle PC1
		TCNT2 = 0x00;	
	}
}